#include <iostream>
#include <thread>
#include <chrono>
#include <future>
using namespace std;

#include "threadpool.h"

int sum1(int a, int b)
{
    this_thread::sleep_for(chrono::seconds(3));
    return a + b;
}
int sum2(int a, int b, int c)
{
    this_thread::sleep_for(chrono::seconds(3));
    return a + b + c;
}


int main()
{
    ThreadPool pool;
    // pool.setMode(PoolMode::MODE_CACHED);
    pool.start(2);
    future<int> r1 = pool.submitTask(sum1, 10, 20);
    future<int> r2 = pool.submitTask(sum2, 100000000, 200000000, 300000000);
    future<int> r3 = pool.submitTask([](int l, int r)->int {
        int sum = 0;
        for(int i = l; i <= r; ++i) sum += i;
        return sum;
    }, 1, 100);
    
    future<int> r4 = pool.submitTask([](int l, int r)->int {
        int sum = 0;
        for(int i = l; i <= r; ++i) sum += i;
        return sum;
    }, 1, 100);
    
    future<int> r5 = pool.submitTask([](int l, int r)->int {
        int sum = 0;
        for(int i = l; i <= r; ++i) sum += i;
        return sum;
    }, 1, 100);
    
    cout << r1.get() << endl;
    cout << r2.get() << endl;
    cout << r3.get() << endl;
    cout << r4.get() << endl;
    cout << r5.get() << endl;

#if 0
    packaged_task<int(int, int)> task(sum1);
    future<int> ret = task.get_future();    // 获取任务返回值
    // task(10, 20);
    thread t(move(task), 10, 20);
    t.detach();
    cout << ret.get() << endl;
    return 0;
#endif
}
